package com.sghd.football.utils.encrypt;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.Properties;

public class PropUtil {
	private static Properties prop;
	private static String md5 = "7908081a4581f6c0a817e8422acf087d";
	
	private static void getInstance(){
		if (prop == null){
			try {
				prop = new Properties();
				InputStream ins = PropUtil.class.getResourceAsStream("/config.properties");
				prop.load(ins);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static String getProperty(String key){
		if (prop == null) getInstance();
		return prop.getProperty(key);
	}
	
	public static void refreshProp(){
		try {
			byte buffer[] = new byte[1024];
			int len;
			MessageDigest digest = MessageDigest.getInstance("MD5");
			InputStream in = PropUtil.class.getResourceAsStream("/config.properties");
			while ((len = in.read(buffer, 0, 1024)) != -1) 
			{
				digest.update(buffer, 0, len);
			}
			in.close();
			
			BigInteger bigInt = new BigInteger(1, digest.digest());
			String m = bigInt.toString(16);
			
			if (!md5.equals(m))
			{	
				md5 = m;
				prop = null;
				getInstance();
				System.out.println(md5);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
